import { Component, OnInit, Inject, forwardRef, ChangeDetectorRef, ViewChild } from '@angular/core';
import { FileUploader } from '../../../../../lib/ng2-file-upload';
import { TabsetComponent } from 'ngx-bootstrap';
import { AppComponent } from '../../../../app.component';

import { masterService } from '../../../../../lib/master.service';
import { saveVarService } from '../../../../../lib/saveVar.service'
import { path } from '../../../../path.config';
import { NzModalService } from 'ng-zorro-antd';


@Component({
    selector: 'app-exchange-card-add',
    templateUrl: './exchange-card-add.component.html',
    styleUrls: ['./exchange-card-add.component.css'],
    providers: [forwardRef(() => AppComponent)]
})
export class ExchangeCardAddComponent implements OnInit {
    @ViewChild('exchangeCardAdd') private exchangeCardAdd: TabsetComponent;

    // api
    // 获得模版信息
    public getListInfo: any = path.cardManagement.batchinfo.getListInfo;

    public createInfo : any = path.cardManagement.batchinfo.createBatchInfo;

    public templates: Array<any> = [];

    public template : any = {
        generateCount : 0
    };


    public ExchangeCardAddModel = {
        templateId : 0,
        count : 0,
        channel : ''
    };

    public count = 0;

    constructor(private _masterService: masterService,
        private _saveVar: saveVarService,
        private cdRef: ChangeDetectorRef,
        private confirmServ: NzModalService,
        @Inject(forwardRef(() => AppComponent)) public app: AppComponent) { }

    ngOnInit() {
        this._masterService
            .getData(this.getListInfo)
            .subscribe((res) => {
                if (res.statusCode == 200) {
                    this.templates = res.data;
                    this.cdRef.detectChanges();
                } else if (res.statusCode == 401) {
                    this.app.logOff();
                } else {
                    this.app.toastError('错误信息：' + res.errorMessage);
                }
                $("#templateId").val(-1);
            })
    }

    changeTemplate(){
        var index = parseInt($("#templateId").val().toString());
        if(index >= 0)
        {
            this.template = this.templates[index];
        }
        else
        {
            this.template = { generateCount : 0 };
        }
    }

    saveCardBatchinfo(){
        var index = parseInt($("#templateId").val().toString());
        if(index < 0)
        {
            this.app.toastError('错误信息：请选择卡券模版!');
            return;
        }
        if(!!!this.ExchangeCardAddModel.channel || this.ExchangeCardAddModel.channel == '')
        {
            this.app.toastError('错误信息：请填写渠道!');
            return;
        }
        if(!!!this.ExchangeCardAddModel.count || !$.isNumeric(this.ExchangeCardAddModel.count) || this.ExchangeCardAddModel.count <= 0)
        {
            this.app.toastError('错误信息：请填写正确的生成卡券数量!');
            return;
        }
        this.template = this.templates[index];        
        if(this.ExchangeCardAddModel.count > this.template.generateCount)
        {
            this.app.toastError('错误信息：需要生成卡券数量不能多于可领数量!');
            return;
        }        
        this.ExchangeCardAddModel.templateId = this.template.id;
        this._masterService
            .postData(this.createInfo, this.ExchangeCardAddModel)
            .subscribe((res) => {
                if (res.statusCode == 200) {
                    //this.templates = res.data;
                    this.cdRef.detectChanges();
                    this.app.toastSuccess("生成成功");
                } else if (res.statusCode == 401) {
                    this.app.logOff();
                } else {
                    this.app.toastError('错误信息：' + res.errorMessage);
                }
                $("#templateId").val(-1);
            })
    }
}
